package com.wallpaper.weatherwallpaper;

import com.model.*;
import com.model.location.City;
import com.model.location.Province;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.view.View.OnClickListener;

public class SettingActivity extends Activity {
	// widgets
	private Button setWeatherWallPaperButton;
	private Button setCityButton;
	private ToggleButton screenSaverToggleButton;
	private EditText autoIntervalField;
	private EditText dayStartTimeHourField;
	private EditText dayStartTimeMinuteField;
	private EditText dayEndTimeHourField;
	private EditText dayEndTimeMinuteField;
	private Button saveButton;
	private TextView cityTextView;
	
	private User user;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        //获得用户
        user = User.getUser(SettingActivity.this);
        initWidgets();
        addListener();
    }
    
    // 刷新已经选择的城市
    public void onResume(){
    	super.onResume();
    	showCityText();
    }
    

    //跳转到其他页面的时候，保存用户设置
    public void onDestroy(){
    	super.onDestroy();
    }
    
    //按下返回键或者home键时，保存用户设置
    public void onPause(){
    	super.onPause();
    	user.saveUserData();
    }
    private void initWidgets(){
    	setWeatherWallPaperButton = (Button) this.findViewById(R.id.SetWeatherWallPaperButton);
    	setCityButton = (Button) this.findViewById(R.id.SetCityButton);
    	
    	//根据用户的选择来初始化设置
    	screenSaverToggleButton = (ToggleButton) this.findViewById(R.id.ScreenSaverToggleButton);
  		screenSaverToggleButton.setChecked(user.isScreenSaver());
  		
  		//初始化时间间隔
    	autoIntervalField = (EditText) this.findViewById(R.id.autoUpdateIntervalTextField);
    	autoIntervalField.setText(String.valueOf(user.getAutoUpdateInterval()));
    	
    	//初始化白天的时间
    	dayStartTimeHourField = (EditText) this.findViewById(R.id.dayStartTimeHourTextField);
    	dayStartTimeHourField.setText(String.valueOf(user.getDayStartTime().getHour()));
    	dayStartTimeMinuteField = (EditText) this.findViewById(R.id.dayStartTimeMinuteTextField);
    	dayStartTimeMinuteField.setText(String.valueOf(user.getDayStartTime().getMinute()));
    	dayEndTimeHourField = (EditText) this.findViewById(R.id.dayEndTimeHourTextField);
    	dayEndTimeHourField.setText(String.valueOf(user.getDayEndTime().getHour()));
    	dayEndTimeMinuteField = (EditText) this.findViewById(R.id.dayEndTimeMinuteTextField);
    	dayEndTimeMinuteField.setText(String.valueOf(user.getDayEndTime().getMinute()));
    	
    	//初始化已经选择的城市
    	cityTextView = (TextView) this.findViewById(R.id.SelectedCityTextView);
    	showCityText();
    	
    	saveButton = (Button) this.findViewById(R.id.SaveButton);
    }
    
    private void showCityText(){
    	Province province = user.getProvince();
    	City city = user.getCity();
    	String showText = "当前选择的城市是： ";
    	if(province.isMunicipalities())
    		showText +=province.getProvinceName();
    	else
    		showText +=city.getCityName();
    	
    	cityTextView.setText(showText);
    }
    
    private void addListener(){
    	setWeatherWallPaperButton.setOnClickListener(new OnClickListener(){
    		public void onClick(View arg0) {
    			// ��ת�����ñ�ֽҳ��		
    			Intent intent = new Intent();
    			intent.setClass(SettingActivity.this, SetWallPaperActivity.class);
    			startActivity(intent);
    			SettingActivity.this.finish();//������ҳ�����ȥ֮��͹ر�����ҳ��
    		}
    	});
    	setCityButton.setOnClickListener(new OnClickListener(){
    		public void onClick(View arg0) {
    			// ��ת�����ó���ҳ��		
    			Intent intent = new Intent();
    			intent.setClass(SettingActivity.this, SetCityActivity.class);
    			startActivity(intent);
    			//SettingActivity.this.finish();//������ҳ�����ȥ֮��͹ر�����ҳ��
    		}
    	});
    	
    	saveButton.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
		    	//先判断是否可以跳转
		    	boolean isValid = validateValues();
		    	if(isValid){
		    		user.saveUserData();
		    		SettingActivity.this.finish();
		    	}
		    	else
		    		return;
			}
    	});
    }
    
    
    /**
     * 在保存之前，检查所有的值是否正确
     * 如果不正确的话，就提示错误，并停留在当前页
     * @return
     */
    private boolean validateValues(){
    	int autoInterval = Integer.valueOf(autoIntervalField.getText().toString());
    	int dayStartHour = Integer.valueOf(dayStartTimeHourField.getText().toString());
    	int dayStartMinute = Integer.valueOf(dayStartTimeMinuteField.getText().toString());
    	int dayEndHour = Integer.valueOf(dayEndTimeHourField.getText().toString());
    	int dayEndMinute = Integer.valueOf(dayEndTimeMinuteField.getText().toString());
    	
    	if(autoInterval>25){
    		autoIntervalField.setText(String.valueOf(user.getAutoUpdateInterval()));
    		Toast.makeText(SettingActivity.this, "请输入24小时内的时间间隔", Toast.LENGTH_SHORT).show();
    		autoIntervalField.requestFocus();
    		return false;
    	}
    	if(dayStartHour>24){
    		dayStartTimeHourField.setText(String.valueOf(user.getDayStartTime().getHour()));
    		Toast.makeText(SettingActivity.this, "不能超过24小时", Toast.LENGTH_SHORT).show();
    		dayStartTimeHourField.requestFocus();
    		return false;
    	}
    	if(dayEndHour>24){
    		dayEndTimeHourField.setText(String.valueOf(user.getDayEndTime().getHour()));
    		Toast.makeText(SettingActivity.this, "不能超过24小时", Toast.LENGTH_SHORT).show();
    		dayEndTimeHourField.requestFocus();
    		return false;
    	}
    	if(dayStartMinute>60){
    		dayStartTimeMinuteField.setText(String.valueOf(user.getDayEndTime().getMinute()));
    		Toast.makeText(SettingActivity.this, "不能超过60分钟", Toast.LENGTH_SHORT).show();
    		dayStartTimeMinuteField.requestFocus();
    		return false;
    	}
    	if(dayEndMinute>60){
    		dayEndTimeMinuteField.setText(String.valueOf(user.getDayStartTime().getMinute()));
    		Toast.makeText(SettingActivity.this, "不能超过60分钟", Toast.LENGTH_SHORT).show();
    		dayEndTimeMinuteField.requestFocus();
    		return false;
    	}
    	
    	if(!compareTime()){
    		Toast.makeText(SettingActivity.this, "白天开始时间不能晚于结束时间", Toast.LENGTH_SHORT).show();
    		return false;
    	}
    	
    	//检验通过，保存用户的值
    	user.setAutoUpdateInterval(autoInterval);
    	user.setDayStartTime(new MyTime(dayStartHour,dayStartMinute));
    	user.setDayEndTime(new MyTime(dayEndHour,dayEndMinute));
    	user.setScreenSaver(screenSaverToggleButton.isChecked());
    	
    	return true;
    }
    
    //对比时间的大小，返回true的时间序列才是正确的
    private boolean compareTime(){
    	MyTime endTime = new MyTime(Integer.valueOf(dayEndTimeHourField.getText().toString()),
    			Integer.valueOf(dayEndTimeMinuteField.getText().toString()));
    	MyTime startTime = new MyTime(Integer.valueOf(dayStartTimeHourField.getText().toString()),
    			Integer.valueOf(dayStartTimeMinuteField.getText().toString()));
    	if(MyTime.compareTime(endTime, startTime)==1)
    		return true;
    	else
    		return false;
    }
}
